#include <iostream>
#include <vector>
#include <list>

#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>
#include <TH1D.h>
#include <TCanvas.h>
#include <TVector3.h>

#include "GeomSvc.h"
#include "TriggerRoad.h"
#include "TriggerAnalyzer.h"

using namespace std;

int main(int argc, char* argv[])
{
  GeomSvc* p_geomSvc = GeomSvc::instance();
  p_geomSvc->init("geometry_R962");

  double cut1 = 0.;
  double cut2 = 0.;
  double cut3 = 1E6;
  if(argc > 2) cut1 = atof(argv[2]);
  if(argc > 3) cut2 = atof(argv[3]);
  if(argc > 4) cut3 = atof(argv[4]);
  TriggerAnalyzer* triggerAna = new TriggerAnalyzer();
  triggerAna->init("roads_DY.root", cut1, cut2, cut3);
  triggerAna->buildTriggerTree();
  triggerAna->outputEnabled();

  int nHits;
  int detectorID[500];
  int elementID[500];

  TFile* dataFile = new TFile(argv[1], "READ");
  TTree* dataTree = (TTree*)dataFile->Get("save");

  dataTree->SetBranchAddress("nHits", &nHits);
  dataTree->SetBranchAddress("detectorID", detectorID);
  dataTree->SetBranchAddress("elementID", elementID);

  int nAccepted = 0;
  for(int i = 0; i < dataTree->GetEntries(); i++)
    {
      dataTree->GetEntry(i);
      if(triggerAna->acceptEvent(nHits, detectorID, elementID))
	{
	  ++nAccepted;
	}
    }

  cout << nAccepted << " / " << dataTree->GetEntries() << endl;

  delete triggerAna;

  return 1;
}
